package web;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import dto.Pro_In_Cart;
import entity.Cart;
import entity.Product;
import entity.User;
import enums.travelenums;
import service.Cart_service;
import service.Product_service;

@Controller
public class Cart_controller {

	@Autowired
	private Cart_service cart_service;
	@Autowired
	private Product_service product_service;
	
	@RequestMapping(value="user/addcart",method=RequestMethod.POST)
	@ResponseBody
	public String AddToCart(@RequestParam("productId") Integer productId,
							@RequestParam("quantity") Integer quantity,@RequestParam("date") String date,
							HttpServletRequest request) throws Exception{
	
		User user=(User)request.getSession().getAttribute("user");
		if(user==null){
			return travelenums.NOLOGIN.getInfo();
		}else {
			SimpleDateFormat sd=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String time=sd.format(new Date());
			
			Cart cart = new Cart(user.getId(),productId,quantity,time,time,date);
			boolean boo=cart_service.add_ToCart(cart);
			if(boo){
				return travelenums.SUCCESS.getInfo();
			}else{
				return travelenums.ERRO.getInfo();
			}
		}	
	}
	
	@RequestMapping(value="user/cartlist")
	public String cartlist(@RequestParam( value="page",required=true,defaultValue="1")Integer page,HttpServletRequest request,Model model){
		User user= (User)request.getSession().getAttribute("user");		
		Page<Pro_In_Cart> pa=PageHelper.startPage(page, 3);
		List<Pro_In_Cart> cartlist=cart_service.ProInCart_list(user.getId());
		PageInfo<Pro_In_Cart> p=new PageInfo<Pro_In_Cart>(pa);
		model.addAttribute("page", p);
		model.addAttribute("cartlist", cartlist);
		return "cartlist";
	}
	
	@RequestMapping(value="user/remove_pro")
	public String remove_pro(@RequestParam("productId") Integer productId,
								HttpServletRequest request,Model model) throws Exception{
			
		User user=(User)request.getSession().getAttribute("user");
		PageHelper.startPage(1, 3);
		cart_service.remove_FromCart(productId, user.getId());
		
		List<Pro_In_Cart> cartlist=cart_service.ProInCart_list(user.getId());
		PageInfo<Pro_In_Cart> p=new PageInfo<Pro_In_Cart>(cartlist);
		model.addAttribute("page", p);
		model.addAttribute("cartlist", cartlist);
		return "cartlist";
	}
	
	@RequestMapping (value="user/figure",method=RequestMethod.POST)
	public String figure_oder(HttpServletRequest request,Model model){
		
		String arr_productId=request.getParameter("arr_proid");
		String arr_quantity=request.getParameter("arr_quantity");
		String[] productId_arr=arr_productId.split(",");
		String[] quantity_arr=arr_quantity.split(",");	
	
		User user=(User)request.getSession().getAttribute("user");
		List<Pro_In_Cart> list=new ArrayList<Pro_In_Cart>();
		for(int i=0;i<productId_arr.length;i++){
			Integer productId=Integer.parseInt(productId_arr[i]);
			Integer quantity=Integer.parseInt(quantity_arr[i]);
			
			Cart cart=cart_service.getcartByid(user.getId(), productId);
			String date=cart.getDate();
			
			Product product= product_service.getproByid(productId);
			String description= product.getProDescription();
			double pro_price=product.getProPrice();
			double pro_totalprice=pro_price*quantity;
			
			Pro_In_Cart pro_In_Cart=new Pro_In_Cart(productId,description,quantity,pro_totalprice,date);
			list.add(pro_In_Cart);
		}
		model.addAttribute("prolist",list);
		return "confirm";
	}
	
	
}
